package com.yunshipei.wps;

import java.io.File;
import java.io.InputStream;
import java.util.Locale;

import org.apache.http.util.EncodingUtils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public class Util
{

  	// 检测该包名所对应的应用是否存在
	public static boolean checkPackage(Context context, String packageName) {
	    if (packageName == null || "".equals(packageName)) {
	    	return false; 
	    }
	    try {  
	    	context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);  
	        return true;  
	    } 
	    catch (NameNotFoundException e) {  
	        return false;  
	    }  
	}  
  	
  	public static void showToast(Context context, String content) {
  		showToast(context, content, Toast.LENGTH_LONG);
  	}
  	
  	public static void showToast(final Context context, final String content, final int length_short) {
  		Handler handle = new Handler(Looper.getMainLooper());
  		handle.post(new Runnable() {
			@Override
			public void run() {
				Toast.makeText(context, content, length_short).show();
			}
  		});  		
  	}
  	
    public static String getMIMEType(File f) {  
        String end = f.getName().substring(f.getName().lastIndexOf(".") + 1,  
                		f.getName().length()).toLowerCase(Locale.getDefault());  
        String type = "";  
        if (end.equals("mp3") || end.equals("aac") || end.equals("aac") 
        		|| end.equals("amr") || end.equals("mpeg") || end.equals("mp4"))  
        {  
          type = "audio";  
        }
        else if (end.equals("jpg") || end.equals("gif") || end.equals("png") || end.equals("jpeg"))  
        {  
          type = "image";  
        } 
        else if (end.equals("doc") || end.equals("docx") || end.equals("pdf")
        		|| end.equals("txt"))
        {  
          type = "application/msword";  
          return type;
        }  
        else
        {
        	type = "*";
        }
        type += "/*";  
        return type;  
    } 
    
	/**
  	 * 判断是否是wps能打开的文件
  	 * @param file
  	 * @return
  	 */
  	public static boolean IsWPSFile(File file) {
  		String end = file.getName().substring(file.getName().lastIndexOf(".") + 1,  
        		file.getName().length()).toLowerCase(Locale.getDefault());  
  		if (end.equals("doc") || end.equals("dot") || end.equals("wps") 
  			|| end.equals("wpt") || end.equals("docx") || end.equals("dotx")
  			|| end.equals("docm") || end.equals("dotm") || end.equals("rtf") 
			|| end.equals("xls") || end.equals("xlt") || end.equals("et")
			|| end.equals("ett") || end.equals("xlsx") || end.equals("xltx")
			|| end.equals("csv") || end.equals("xlsb") || end.equals("xlsm")
			|| end.equals("xml") || end.equals("html") || end.equals("htm")
			|| end.equals("ppt") || end.equals("pptx") || end.equals("dps")
			|| end.equals("pot") || end.equals("pps") || end.equals("dpt")
			|| end.equals("potx") || end.equals("ppsx") || end.equals("pptm")
			|| end.equals("txt") || end.equals("pdf") 
			|| end.equals("potm") || end.equals("ppsm"))
  			return true;
  		
  		return false;
  	}
  	
  	public static boolean isPDFFile(String filePath) {
  		String path = filePath.toLowerCase(Locale.getDefault());
		return path.endsWith(".pdf");
  	}
  	
  	public static boolean isPPTFile(String filePath) {
  		filePath = filePath.toLowerCase(Locale.getDefault());
  		return filePath.endsWith(".ppt")
  				|| filePath.endsWith("pptx");
  	}
  	
  	public static boolean isExcelFile(String filePath) {
  		filePath = filePath.toLowerCase(Locale.getDefault());
  		return filePath.endsWith(".xls")
  				|| filePath.endsWith(".xlsx")
  				||filePath.endsWith(".et");
  	}
  	
  	public static boolean isWordFile(String filePath) {
  		filePath = filePath.toLowerCase(Locale.getDefault());
  		return filePath.endsWith(".doc")
  				|| filePath.endsWith("docx");
  	}
  	
  	public static boolean isTxtFile(String filePath) {
  		filePath = filePath.toLowerCase(Locale.getDefault());
  		return filePath.endsWith(".txt");
  	}
  	
  	public static String assetsFileRead(String fileName, Context context) {
		String ret = "";
		try {
			InputStream is = context.getAssets().open(fileName);
			int len = is.available();
			byte[] buffer = new byte[len];
			is.read(buffer);
			ret = EncodingUtils.getString(buffer, "utf-8");
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
}
